package com.chenfan.ccp.entity;

import com.chenfan.ccp.plug.mybatis.handler.DbHandler;
import com.chenfan.ccp.plug.mybatis.annotation.Column;
import com.chenfan.ccp.plug.mybatis.annotation.Id;
import com.chenfan.ccp.plug.mybatis.mapper.BaseMapper;

import java.io.Serializable;
import java.util.Date;

public class BaseEntity<T> extends BaseMapper<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    private static final int PAGE_SIZE = 10;
    private static final String PAGE_SYMBOL = " LIMIT %d , %d";
    private static final ThreadLocal<String> PAGE_LOCAL = new InheritableThreadLocal();
    @Id
    protected Long id;
    @Column(dateGeneStrategy = DbHandler.DateGeneStrategy.CREATE)
    protected Date createTime;
    @Column(dateGeneStrategy = DbHandler.DateGeneStrategy.UPDATE)
    protected Date updateTime;
    @Column(commit = "物理删除开关 true 删除 false 不删除 默认为false", defaultVal = "false")
    protected Boolean deletedFlag;

    public static void setPage(Integer start, Integer count) {

        PAGE_LOCAL.set(String.format(PAGE_SYMBOL, ((start <= 1 ? 1 : start) - 1) * count, count));
    }

    public static String getPage() {
        String pageStr = PAGE_LOCAL.get();
        if (pageStr != null) {
            PAGE_LOCAL.remove();
        }
        return pageStr;
    }

    public static void setPage(Integer start) {
        setPage(start, PAGE_SIZE);
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    public Boolean getDeletedFlag() {
        return deletedFlag;
    }

    public void setDeletedFlag(Boolean deletedFlag) {
        this.deletedFlag = deletedFlag;
    }
}
